package com.vincent.kdp.Activity;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.vincent.kdp.PostUrl;
import com.vincent.kdp.R;
import com.vincent.kdp.js.JsOperator;
import com.vincent.kdp.js.VipInfoOperator;
import com.vincent.kdp.ui.bean.entity.VipInfo;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConnectUhfActiviy extends AppCompatActivity {

    @Bind(R.id.back_img)
    ImageView mBackImg;
    @Bind(R.id.toolbar_center_tv)
    TextView mToolbarCenterTv;
    @Bind(R.id.webView)
    WebView webView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_connect_uhf_activiy);
        ButterKnife.bind(this);
        mToolbarCenterTv.setText("设置盘点拍");
        this.initializeWebView();
    }


    @SuppressLint({ "NewApi", "SetJavaScriptEnabled" })
    private void initializeWebView(){
        webView.addJavascriptInterface(new JsOperator(ConnectUhfActiviy.this),
                "ConnectUhf");
        try {
            //String url = "file:///android_asset/LoginJs/login.html";
            String url= PostUrl.SERVICE_URL+"/H5/connect/connect.html";
            WebSettings webSettings = webView.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setAllowFileAccess(true);
            webSettings.setAllowFileAccessFromFileURLs(true);
            setClient();
            this.webView.loadUrl(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(R.id.back_img)
    public void onClick() {
        this.finish();
        overridePendingTransition(com.vincent.baseapp.R.anim.fragment_left_in, com.vincent.baseapp.R.anim.fragment_right_out);
    }

    private  void setClient(){
        webView.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
                AlertDialog.Builder b2 = new AlertDialog.Builder(ConnectUhfActiviy.this).setTitle("通知")
                        .setMessage(message).setPositiveButton("确定", new AlertDialog.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        result.confirm();
                        }
                });
                b2.setCancelable(false);
                b2.create();
                b2.show();
                    return true;
                }
            });
    }
}
